<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\TongSourceContent;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TongSourceContentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TongSourceContent(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->column('u_id','发布人')->display(function($value){
                if($value==0){
                    return '官方发布';
                }else{
                    return $value;
                }
            });
            $grid->column('c_id');
            $grid->column('title');
            $grid->column('content');
            $grid->column('imags','图片')->display(function($value){
                return explode(',',$value)[0];
            })->gallery();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TongSourceContent(), function (Show $show) {
            $show->field('id');
            $show->field('u_id');
            $show->field('c_id');
            $show->field('title');
            $show->field('content');
            $show->field('imags');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TongSourceContent(), function (Form $form) {
            $form->radio('c_id','分类')->options(config('tong.su_cate'))->default('0');
            $form->text('title','标题');
            $form->textarea('content','内容');
            $form->multipleImage('imags','商品图片：')
                ->help('上传的第一张图片会作为主图')->saving(function ($paths) {
                    // var_dump(implode(',', $paths));
                    // 可以转化为由 , 隔开的字符串格式
                    return implode(',', $paths);
                    //return '1313123';
                    // 也可以转化为json
                    //return json_encode($paths);
                })->autoUpload();

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();

                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();

                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();

            });
            $form->disableDeleteButton();
            $form->disableResetButton();
        });
    }
}
